<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../layuimini/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuimini/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" id="app">-
    <div class="layuimini-main">

        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
            <legend>添加评论</legend>
        </fieldset>
        <form class="layui-form layui-form-pane" action="" lay-filter="example">
            <div class="layui-form-item">
                <label class="layui-form-label">菜品ID</label>
                <div class="layui-input-inline">
                    <input type="text" name="did" class="layui-input" :value="did">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">菜品名</label>
                <div class="layui-input-inline">
                    <input type="text" name="dname" autocomplete="off" class="layui-input" :value="dname">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">评论</label>
                <div class="layui-input-inline">
                    <input type="text" name="con" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn" lay-submit="" lay-filter="demo2" type="button">发布评论</button>
            </div>

        </form>
        </form>
    </div>
</div>
<script src="../../layuimini/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../layuimini/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../js/vue.js" charset="utf-8"></script>
<script>

    var layuiSession = sessionStorage.getItem("addComment");
    var addComment = JSON.parse(layuiSession);
    var userSession = sessionStorage.getItem("user");
    var user = JSON.parse(userSession);

    layui.use(['form', 'layedit', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            layedit = layui.layedit,
            laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            }
            , pass: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ]
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });

        //监听指定开关
        form.on('switch(switchTest)', function (data) {
            layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
                offset: '6px'
            });
            layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
        });

        //监听提交
        form.on('submit(demo2)', function (data) {
            $.ajax({
                url: "http://localhost:8080/order/commentAddServlet",
                type: "POST",
                async: false,
                dataType: "json",
                data: {
                    "did": data.field.did,
                    "uid": user.id,
                    "con": data.field.con
                },
                success: (res) => {

                    layer.msg('发布评论成功', function () {
                        window.location = '../order.html';
                    });
                },
                error: (err) => {
                    window.location = '404.html';
                }
            })
            return false;
        });

    });


    new Vue({
        el: "#app",
        data() {
            return {
                did: addComment.dishes[0].did,
                dname: addComment.dishes[1].dname,
            }
        }

    })


</script>
</body>
</html>
